<?php
/**
 * Created by PhpStorm.
 * User: chaichunmin
 * Date: 2017/1/5
 * Time: 13:55
 */

namespace SDXApi\Behaviors;
use Think\Behavior;
use SDXApi\Service\AliyunLogService;
class RecEngBehavior extends Behavior {

    public function run(&$params){
        if(!empty($params)){
            $hook = $params[0];
            if(!empty($hook)){
                $this->$hook($params);
            }
        }
        return;
    }

    /*
     * 写入一条
     */
    protected function insertOne($param){
        D('UserBehavior')->add_user_behavior($param[1], $param[2], $param[3], $param[4], $param[5], $param[6]);
        return;
    }

    /*
     * 写入列表
     */
    protected function isertList($param){
        //购买商品列表
        if($param[2] == 'consume' || $param[2] == 'submit_order'){
            $order_info = M('Order_info')->field('user_id,wid')->where(array('order_id'=>$param[3]))->find();
            $order_goods = M('Order_goods')->field('goods_id,goods_price,goods_number')->where(array('order_id'=>$param[3]))->select();

            foreach ($order_goods as $key=>$value){
                date_default_timezone_set('PRC');
                $storage = M('goods_storage')->field('id')->where(array('goods_id'=>$value['goods_id'],'wid'=>$order_info['wid']))->find();
                $data[$key]['user_id'] = $order_info['user_id'];
                $data[$key]['item_id'] = $value['goods_id'];
                $data[$key]['wid'] = $order_info['wid'];
                $data[$key]['storageid'] = $storage['id'] ? $storage['id'] : 0;
                $data[$key]['bhv_type'] = $param[2];
                $data[$key]['bhv_amt'] = $value['goods_price'];
                $data[$key]['bhv_cnt'] = $value['goods_number'];
                $data[$key]['bhv_datetime'] = date('Y-m-d H:i:s');
                $data[$key]['add_time'] = gmtime();

            }

        }else{
            //商品表列表
            foreach ($param[3] as $key=>$value){

                date_default_timezone_set('PRC');
                $storage = M('goods_storage')->field('id')->where(array('goods_id'=>$value['goods_id'],'wid'=>$param[4]))->find();
                $data[$key]['user_id'] = $param[1];
                $data[$key]['item_id'] = $value['goods_id'];
                $data[$key]['wid'] = $param[4];
                $data[$key]['storageid'] = $storage['id'] ? $storage['id'] : 0;
                $data[$key]['bhv_type'] = $param[2];
                $data[$key]['bhv_amt'] = 1;
                $data[$key]['bhv_cnt'] = 1;
                $data[$key]['add_time'] = gmtime();
                $data[$key]['bhv_datetime'] = date('Y-m-d H:i:s');
                if($param[5]) $data[$key]['page_type'] = $param[5];
                if($param[6]) $data[$key]['page_title'] = $param[6];
                if($param[7]) $data[$key]['page_id'] = $param[7];
            }
        }



        if($data){

            //正式环境写入阿里云日志服务
            if($_SERVER['SERVER_NAME'] == 'api.shidexian.com' && $param[2] == 'view'){
                $aliyun_log = new AliyunLogService();
                $aliyun_log->write_log_all($data);

            }else{
                D('UserBehavior')->addAll($data);
            }
        }


        return;
    }
}